#include "maploader.h"
#include "utilities.h"

using namespace::std;

maploader::maploader()
{

}

void maploader::loadMap(std::string mapname)
{
    tmxHandler parser;
    parser.set_substitute_entities(true);//
    parser.parse_file(mapname);
    m_listMap = parser.getMap();
    cerr << "Coucou, tu veux voir ma..." << parser.getImagePath() << endl;
    m_tileSet.load(parser.getImagePath());
    cerr << "finished loading tileSet" << endl;
}

void maploader::draw(graphics *graphs)
{
    cerr <<"draw" << endl;;
    int x;
    int y;
    int l;
    int tileId;
    SDL_Surface* tile;
    SDL_Rect* rect;
    for(l= 0; l<m_listMap.size(); l++)
    {
        for(x=0; x<m_listMap[l].size(); x++)
        {
            for(y=0; y<m_listMap[l][x].size(); y++)
            {
                cerr << "try to get tile" << endl;
                tileId = m_listMap[l][x][y];
                tile = m_tileSet.getTile(tileId);
                rect->x = x*32;
                rect->y = y*32;
                rect->h = 32;
                rect->w = 32;
                graphs->drawImage(tile, rect);
            }
        }
    }
}

maploader::~maploader()
{
    //dtor
}

tileSet::tileSet()
{

}

/* CLASS TMXHANDLER

*/
tmxHandler::tmxHandler()
{
    m_line = 0;
    m_lines = 0;
    m_collumn = 0;
    m_collumns = 0;
    m_layer = 0;
}

tmxHandler::~tmxHandler()
{
	cerr << "je suis appellé =D";
}

std::vector<std::vector<std::vector<int> > > tmxHandler::getMap()
{
    return m_listMap;
}

std::string tmxHandler::getImagePath()
{
    return m_imagepath;
}
void tmxHandler::on_start_element(const Glib::ustring& name, const AttributeList& attributes)
{
    if (name == "map")
    {
        //map! retrieve sizes
        for(xmlpp::SaxParser::AttributeList::const_iterator iter = attributes.begin(); iter != attributes.end(); ++iter)
        {
            if (iter->name == "width")
                m_collumns = ustringToInt(iter->value);
            if (iter->name == "height")
                m_lines = ustringToInt(iter->value);

        }
    }
    if (name == "image")
    {
        //image! retrieve tileset image path
        for(xmlpp::SaxParser::AttributeList::const_iterator iter = attributes.begin(); iter != attributes.end(); ++iter)
        {
            if (iter->name == "source")
                m_imagepath = iter->value.raw();

        }
    }
    if (name == "layer")
    {
        //new layer! reset coords data for new fill in
        m_collumn = 0;
        m_line = 0;
        m_layer++;
        //add layer to Array
        m_listMap.resize(m_layer+1);
        m_listMap[m_layer].resize(m_line+1);
    }
    if (name== "tile")
    {
        int gid;
        //cerr << "y: " << m_line << "/" << m_lines << endl << "x: " << m_collumn << "/" << m_collumns << endl;
        for(xmlpp::SaxParser::AttributeList::const_iterator iter = attributes.begin(); iter != attributes.end(); ++iter)
        {
            if (iter->name == "gid")
            {
                 gid = ustringToInt(iter->value);
            }
        }
        m_collumn++;
        //add gid to last place in Array
        m_listMap[m_layer][m_line].push_back(gid);
        //if we are at end of line add line and restart
;       if (m_collumn >= m_collumns)
            {
                m_collumn = 0;
                m_line++;

                //add lines to Array
                m_listMap[m_layer].resize(m_line+1);
            }
    }
}



/* CLASS TILESET

*/

void tileSet::load(string filename)
{
    cerr << "loading: " << filename << " ..." << endl;
    int height;
    int width;
    int tilesX;
    int tilesY;
    int x(0);
    int y(0);
    int tmpSurfI(0);
    SDL_Surface* tilesetImg;
    SDL_Rect* rect;
    char testImgChar[]="MC_Overworld.PNG";
    //tilesetImg = IMG_Load(filename.c_str());
    tilesetImg = IMG_Load(testImgChar);
    if (!tilesetImg)
    {
        cerr << "error loading tileset image" << endl;
    }
    cerr << "image loaded!" << endl;
    height = tilesetImg->h;
    width = tilesetImg->w;
    tilesX = width/32;
    tilesY = height/32;
    vector<SDL_Surface*> tmpsurf(tilesX*tilesY);
    for(y=1; y<tilesY; y++)
    {
        for(x=1; x<tilesX; x++)
        {
            rect->x = x*32;
            rect->y = y*32;
            rect->w = 32;
            rect->h = 32;
            //cerr << "subsurfacing" << endl;
            tmpsurf[tmpSurfI] = graphics::subSurface(tilesetImg, rect);
            m_tileList.push_back(tmpsurf[tmpSurfI]);
            tmpSurfI++;
            //cerr << "success" << endl;

        }
    }
    cout << "great Success!!" << endl;
}

SDL_Surface* tileSet::getTile(int gid)
{
    cerr << "getting tile:"<< gid << endl;
    SDL_Surface* tile;
    tile = m_tileList[gid];
}
